import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class User {
	public String username;
	public String password;
	public String teacherIP;
	public int userID = 1005;
	public boolean teacher;
	public double averageWPM;
	public double[] allWPM = new double[100];
	public int wpmIndex = 0;
	public String[] newInfo = new String[100];
	
	public User(){
		username = "";
		password = "";
		userID = 1005;
		teacher = false;
		averageWPM = 0.0;
		teacherIP = "0";
	}
	
	public User(String uname, String pw, int uid, boolean permission, String teachIP){
		username = uname;
		password = pw;
		userID = uid;
		teacher = permission;
		averageWPM = 0.0;
		teacherIP = teachIP;
	}
	
	public void userLogin(String userN, String userP, String teachIP){
		String uname, upw;
	    boolean loggedin = true;
	    Cipher cipher = new Cipher();
	    uname = /*cipher.encrypt*/userN;
	    username = userN;
	    upw = /*cipher.encrypt*/userP;
	    password = userP;
	    teacherIP = teachIP;
	    File file;
	    Scanner scanner = new Scanner(System.in);
	    file = new File("UserInfo.txt");
		try {
			scanner = new Scanner(file);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
		FileWriter fstream;
		BufferedWriter out;
		try {
			fstream = new FileWriter("User.txt");
			out = new BufferedWriter(fstream);
			out.write(username);
			out.newLine();
			out.write(password);
			out.newLine();
			out.write(teacherIP);
			out.newLine();
			out.close();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	    /* while(scanner.hasNext()){
	    	if(scanner.hasNext() && scanner.next().equals(uname) && scanner.next().equals(upw)){
	    		//uname = cipher.decrypt(uname);
	    		userID = Integer.parseInt(cipher.decrypt(scanner.next()));
	    		System.out.println("Successfully logged in as " + uname + "! UserID: "+userID);
	    		loggedin = true;
	    		break;
	    	}
	    	else if(!scanner.hasNext())
	    		System.out.println("Incorrect username or password. Access Denied.");
	    } */
	    
	    if(loggedin){
	    	int x=0;
	    	file = new File(userID+".txt");
	    	try {
	    		scanner = new Scanner(file);
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
	    	while(scanner.hasNext()){
	    		newInfo[x] = scanner.next();
	    		x++;
	    	}
	    	username = newInfo[0];
	    	password = newInfo[1];
	    	/* if(newInfo[3].equals("true"))
	    		teacher = true;
	    	else
	    		teacher = false;
	    	averageWPM = Double.parseDouble(newInfo[4]); */
		
	    	file = new File(userID+".txt");
	    	try {
	    		scanner = new Scanner(file);
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
	    	String word;
	    	scanner.next();
	    	scanner.next();
	    	int a = 0;
	    	while(scanner.hasNext()){
	    		scanner.next();
	    		if(scanner.hasNext()){
	    			word = scanner.next();
	    			//System.out.println("Word: "+word);
	    			if(word.contains(".")){
	    				allWPM[a] = Double.parseDouble(word);
	    				//System.out.println("Saved in allWPM["+a+"]"+user.allWPM[a]);
	    				wpmIndex++;
	    			}
	    			System.out.println("Trial "+(a+1)+": "+allWPM[a]); //REMOVE 
	    		}
	    		a++;
	    	}
		}
	}
	
	public void userLogout(){
		int x = 4;
		newInfo[x] = Double.toString(averageWPM);
		System.out.println(newInfo[x]);
		x++;	
		for(int q=0;q<wpmIndex;q++){
			newInfo[x] = Integer.toString(q+1);
			System.out.println(newInfo[x]);
			x++;
			newInfo[x] = Double.toString(allWPM[q]);
			System.out.println(newInfo[x]);
			x++;
		}
		calcAverageWPM();
		System.out.println("New averageWPM " + averageWPM);
		FileWriter fstream;
		BufferedWriter out;
		try {
			fstream = new FileWriter("UserNew.txt");
			out = new BufferedWriter(fstream);
			out.write(username);
			out.newLine();
			out.write(password);
			out.newLine();
			//out.write(Integer.toString(userID));
			//out.newLine();
			//out.write(Boolean.toString(teacher));
			//out.newLine();
			//out.write(Double.toString(averageWPM));
			//out.newLine();
			for(int y=0;y<wpmIndex;y++){
				out.write(Integer.toString(y));
				out.newLine();
				out.write(Double.toString(allWPM[y]));
				out.newLine();
			}
			out.close();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	}
	
	public void calcAverageWPM(){
		averageWPM = 0.0;
		int x = 0;
		int counter = 0;
		while(x<allWPM.length){
			if(allWPM[x]!=0){		
				averageWPM += allWPM[x];
				counter++;
			}
			x++;
		}
		averageWPM = averageWPM / (counter);
		DecimalFormat myFormatter = new DecimalFormat("###.##");
		averageWPM = Double.parseDouble(myFormatter.format(averageWPM));
	}
}
